<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    {
        let a, b, c;
        [a, b, ...c] = [1, 2, 3, 4, 5, 6];
        console.log(a, b, c);
    }
    {
        /** 对象解构赋值 */
        let a, b;
        ({a, b} = {a: 1, b: 2});
        console.log(a, b);
    }
    {
        let {x: y = 3} = {x: 5};
        console.log(y);/*5*/
    }
    {
        let a, b, c;
        [a, b, c = 3] = [1, 2];
        console.log(a, b, c);
    }
    {
        /** 变量交换 */
        let a = 1, b = 2;
        [a, b] = [b, a];
        console.log(a, b);
    }
    {
        let [foo, [[bar], baz]] = [1, [[2], 3]];
        console.log(foo, bar, baz); // 1,2,3
    }
    {
        let [a, [b], d] = [1, [2, 3], 4];
        console.log(a, b, d);// 1 2 4
    }
    {
        function fn() {
            return [1, 2];
        }

        let a, b;
        [a, b] = fn();
        console.log(a, b);
    }
    {
        function f1() {
            return [1, 2, 3, 4, 5];
        }

        let a, b, c;
        [a, , , b] = f1();
        console.log(a, b);
    }
    {
        function f2() {
            return [1, 2, 3, 4, 5];
        }

        let a, b, c;
        [a, , ...b] = f2();
        console.log(a, b);
    }
    {
        let obj = {a: 1, b: true};
        let {a, b} = obj;
        console.log(a, b);
    }

    {
        let {a = 1, b = 4} = {a: 2};
        console.log(a, b);
    }

    {
        let data = {
            name: 'apple',
            fruit: [{
                name: 'fruit',
                desc: 'description'
            }]
        };
        let {name: fruitName, fruit: [{name: describe}]} = data;
        console.log(fruitName, describe);
    }
    {
        let [x, y, z] = new Set(["a", "b", "c"]);
        console.log(x, y, z);/*a b c*/
    }
    {
        function* fibs() {
            let a = 0;
            let b = 1;
            while (true) {
                yield a;
                [a, b] = [b, a + b];
            }
        }

        let [first, second, third, fourth, fifth, sixth] = fibs();
        console.log(first, second, third, fourth, fifth, sixth);/*0 1 1 2 3 5*/
    }
    {
        const [a, b, c, d, e] = 'hello';
        console.log(a, b, c, d, e);/*h e l l o*/
    }
    {
        let {length: len} = 'hello';
        console.log(len);/*5*/
    }
    {
        console.log([[1, 2], [3, 4]].map(([a, b]) => a + b));/*[3, 7]*/
    }
    {
        /**变量交换*/
        function example() {
            return [1, 2, 3];
        }

        let [a, b, c] = example();
        console.log(a, b, c); // 1 2 3
    }
    {
        /**返回一个对象*/
        function returnObj() {
            return {
                foo: 1,
                bar: 2
            };
        }

        let {foo, bar} = returnObj();
        console.log(foo, bar);// 1 2
    }
    {
        /**提取json数据*/
        let jsonData = {
            id: 42,
            status: "OK",
            data: [867, 5309]
        };
        let {id, status, data: number} = jsonData;
        console.log(id, status, number);// 42 "OK" [867, 5309]
    }
    {
        /**遍历map数据*/
        let map = new Map();
        map.set('first', 'hello');
        map.set('second', 'world');
        for (let [key, value] of map) {
            console.log(key + " is " + value);//first is hello,second is world
        }
        for (let [key] of map) {
            console.log(key); // first second
        }
    }
</script>
</body>
</html>